/*本模块功能:问答(老师)
        编写者:杨锡君
        编写时间:20180517*/
$(function() {
    var Alert1 = new Alert();
    var loading1 = new Loading();//监控全局ajax添加loading
    loading1.init();
    var confirm1 = new Confirm();
    var th = '<tr>' +
        '<th  class="col-lg-1"><input type="checkbox" name="controlAll" id="controlAll" class="allCheck"></th>'+
        '<th class="col-lg-3">院系</th>' +
        '<th class="col-lg-3">专业</th>' +
        '<th class="col-lg-1">学号/工号</th>' +
        '<th class="col-lg-1">姓名</th>' +
        '<th class="col-lg-1">性别</th>' +
        '<th class="col-lg-1">身份</th>' +
        '</tr>';
    function tabelModel(opt) {//列表内容
        return '<tr>' +
            '<td  class="col-lg-1"><input   value=\"0\" id=' + opt.stuorteaId + ' name=\"selecteds\"  type=\"checkbox\" class=\"oneCheck\" /></td>'+
            '<td class="col-lg-3"  title='+ opt.departmentName +' >'+ opt.departmentName +'</td>' +
            '<td class="col-lg-3" title='+ opt.professionName +'>' + opt.professionName + '</td>' +
            '<td class="col-lg-1" title='+  opt.stuorteaId +'>' + opt.stuorteaId + '</td>' +
            '<td class="col-lg-1" title='+ opt.userName +'>' + opt.userName + '</td>' +
            '<td class="col-lg-1" title='+ opt.sexName +'>' +opt.sexName + '</td>' +
            '<td class="col-lg-1" style="text-align: center"><span >' + opt.roleName + '</span></td>' +
            '</tr>';
    }
    var something;
    var index = {
        init: function() {
            this.bind();
        },
        bind: function() {
            var self = this;
            var json = {
                "pageNow": 1,
                "pageSize": 10
            };
            self.postData(json);
            $(".searchBtn").off("click").on("click", function() {
                var userName = $(".keyword").val();
                var jsonStr = {
                    "pageNow": 1,
                    "pageSize": 10
                };
                if(userName != '') {
                    jsonStr.userName = userName;
                }
                self.postData(jsonStr);
                something = jsonStr;
            });
            $(".close").off("click").on("click", function() {
                self.delEvent();
            });
        },
        delEvent:function () {
            if($(".oneCheck[value=1]").length == 0) {
                Alert1.content = "请选择要解除禁言的人员";
                Alert1.init();
                Alert1.show();
            } else {
                confirm1.content = "确认将选中的人员解除禁言吗？";
                confirm1.success = function () {
                    var list = [];
                    for(var i = 0; i < $(".oneCheck[value=1]").length; i++) {
                        list.push($(".oneCheck[value=1]").eq(i).attr("id"));
                    }
                    var jsons = JSON.stringify({"list":list,"excuseMark":0});
                    loading1.show();
                    $.ajax({
                        type: "post",
                        url: "excuseProblem",
                        data: {
                            "jsonStr":jsons
                        },
                        success: function(msg){
                            loading1.hide();
                            msg = JSON.parse(msg);
                            if(msg.result == "success"){
                                Alert1.content = "解除禁言成功";
                                Alert1.success = function () {
                                    var json = JSON.stringify({
                                        "pageName":"adminBlack"
                                    });
                                    window.location.href = "skipPage?jsonStr="+json;
                                };
                                Alert1.init();
                                Alert1.show();

                            }else{
                                if(msg.errorMsg != "" && msg.errorMsg != undefined){
                                    Alert1.content = msg.errorMsg;
                                }else{
                                    Alert1.content = "解除禁言失败";
                                }
                                Alert1.init();
                                Alert1.show();
                            }
                        }
                    });
                };
                confirm1.init();
                confirm1.show();

            }
        },
        postData: function(jsons) { //获得搜索及列表数据
            var self = this;
            var parameter = jsons;
            loading1.show();
            $.ajax({
                type: "post",
                url: "blackList",
                async: true,
                data: {
                    "jsonStr": JSON.stringify(parameter)
                },
                success: function(data, status, xhr) {
                    loading1.hide();
                    $(".mainContent .tableBox").empty();
                    var msg = JSON.parse(data);
                    if(msg.result == "success"){
                        if(msg.memberList.length != 0) {
                            //列表
                            var tabel = "";
                            $.map(msg.memberList, function(opt) {
                                tabel += tabelModel(opt);
                            });
                            $(".mainContent .tableBox").append(th + tabel);
                            self.allCheckEvent();

                        } else {
                            var trImg = "<tr><td colspan='7'><img style='position:relative;margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td></tr>";
                            $(".mainContent .tableBox").append(th + trImg)
                        }

                        var sumpage = msg.totalPage;
                        if(sumpage == 0){
                            sumpage = 1;
                        }
                        var nowpage = 1;
                        var pagenumber = 10;
                        $(".tcdPageCode").createPage({
                            pageCount: sumpage, //总页数
                            current: nowpage, //当前页数
                            pageNumber: pagenumber, //每页显示数量
                            backFn: function(p) {

                                nowpage = p;

                                if(nowpage >= sumpage) {
                                    nowpage = sumpage;
                                }
                                var jsonStr = jsons;
                                jsonStr.pageNow = nowpage;
                                loading1.show();
                                $.ajax({
                                    type: "post",
                                    url: "blackList",
                                    async: true,
                                    data: {
                                        "jsonStr": JSON.stringify(parameter)
                                    },
                                    async: true,
                                    success: function(msg) {
                                        loading1.hide();
                                        $(".mainContent .tableBox").empty();
                                        var msg = JSON.parse(msg);
                                        if(msg.memberList.length != 0) {
                                            //列表
                                            var tabel = "";
                                            $.map(msg.memberList, function(opt) {
                                                tabel += tabelModel(opt);
                                            });
                                            $(".mainContent .tableBox").append(th + tabel);
                                            self.allCheckEvent();

                                        } else {
                                            var trImg = "<tr><td colspan='7'><img style='position:relative;margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td></tr>";
                                            $(".mainContent .tableBox").append(th + trImg)
                                        }
                                    },
                                    error: function(data, status, xhr) {
                                        loading1.hide();
                                        var json = JSON.stringify({
                                            "pageName": "error"
                                        });
                                        window.location.href = "skipPage?jsonStr=" + json;

                                    }
                                });
                            }
                        });

                    }else {
                        if(msg.failMsg != undefined){
                            if(msg.failMsg == "暂无权限"){
                                parent.location.reload(true);
                            }else{
                                Alert1.content = msg.failMsg;
                                Alert1.init();
                                Alert1.show();
                            }
                        }else{
                            if (msg.errorMsg != "" && msg.errorMsg != undefined) {
                                Alert1.content = msg.errorMsg;
                            } else {
                                Alert1.content = "获取失败";
                            }
                            Alert1.init();
                            Alert1.show();
                        }
                    }

                },
                error: function(data, status, xhr) {
                    loading1.hide();
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;

                }
            });
        },
        allCheckEvent: function() {
            //全选
            allCheck.init("controlAll", "oneCheck");
        }

    };
    index.init();

});